/**
 * 列表服务
 *
 * @author TianGanLin
 * @version 1.0.0, 2017/11/23
 * @see TemplateController
 * @since 模板
 */

(function (app, ng) {
    'use strict';

    /**
     * 注册服务
     */
    app.service('template.service', Service);

    /**
     * 依赖项
     */
    Service.$inject = ['iResource','$http'];

    // 常量声明
    var STATUS = { // 状态
        CREATE: {name: '已创建', value: 1, color: 'primary'},
        COMMIT: {name: '已提交', value: 2, color: 'info'},
        THROUGH: {name: '已通过', value: 3, color: 'success'},
        REFUSE: {name: '已打回', value: 4, color: 'danger'}
    };

    /**
     * 声明服务
     * @constructor
     */
    function Service(iResource,$http) {
        // 初始化
        var s = this;
        extendResource('demos');

        // 属性声明
        this.STATUS = STATUS; // 计划状态

        // 方法引用
        this.statusObject = statusObject; // 获取状态对象
        this.tableOptions = tableOptions; // 获取表格配置

        // 继承RESTful服务
        function extendResource(moduleName, id) {
            id = id || 'id';
            var resource = iResource({
                url: moduleName + '/:' + id, actions: {
                    test: {
                        url: 'demos/test',
                        method: 'POST',
                        contentType: 'json'
                    }
                }
            });
            ng.extend(s, resource);
        }

        /**
         * 获取状态对象
         * @param status
         * @returns {*}
         */
        function statusObject(status) {
            return iUtil.findObject(STATUS, status);
        }

        /**
         * 获取表格配置
         * @param params
         */
        function tableOptions(params) {
            var _gridWidth = 1024;
            var _columnWidth = _gridWidth / 7;
            return {
                url: ISS.getPath('/demos/paging'),
                dataType: 'json',
                extParam: params,
                usepager: true,
                showcheckbox: true,
                pageMode: 'jpa',
                rp: 10,
                isDragCol: true,
                colModel: [
                    {display: 'demo.attribute1', name: 'name', width: _columnWidth * 1.18},
                    {display: 'demo.attribute2', name: 'creator.account', width: _columnWidth},
                    {display: 'demo.attribute3', name: 'date', width: _columnWidth},
                    {
                        display: 'demo.status', name: 'status', width: _columnWidth,
                        process: function (value) {
                            for (var key in STATUS) {
                                var status = STATUS[key];
                                if (status.value === value)
                                    return status.name;
                            }
                            return '未知状态';
                        }
                    },
                    {
                        display: 'common.operation', name: "id", width: 200, process: function (value, index, module) {
                        var html = [];
                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.detail(' + value + ')">');
                        html.push('<span translate="common.read"></span>');
                        html.push('</button>');
                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.edit(' + value + ')">');
                        html.push('<span translate="common.edit"></span>');
                        html.push('</button>');
                        html.push('<button class="btn m-r-xs btn-xs btn-danger" ng-click="vm.remove(' + index + ')">');
                        html.push('<span translate="common.del"></span>');
                        html.push('</button>');
                        return html.join('');
                    }
                    }
                ]
            };
        }
    }
})(iApp.module(), window.angular);